<template>
  <header class="header">
    <h1>todos</h1>
    <input id="toggle-all" class="toggle-all" type="checkbox" v-model="All" />
    <label for="toggle-all"></label>
    <input
      class="new-todo"
      placeholder="输入任务名称-回车确认"
      autofocus
      v-model="content"
      @keydown.enter="add"
    />
  </header>
</template>

<script>
import { mapGetters, mapMutations, mapState } from "vuex";
export default {
  data() {
    return {
      content: "",
    };
  },
  computed: {
    ...mapState("todos", ["list"]),
    ...mapGetters("todos", ["newList"]),
    All: {
      get() {
        return (
          this.newList.length !== 0 && this.newList.every((item) => item.isDone)
        );
      },
      set(val) {
        this.Allchange({
          newList: this.newList,
          val,
        });
      },
    },
  },

  methods: {
    ...mapMutations("todos", ["Allchange", "addItem"]),
    add() {
      this.addItem(this.content);
      this.content = "";
    },
  },

  watch: {
    list: {
      deep: true,
      handler() {
        localStorage.setItem("todos", JSON.stringify(this.list));
      },
    },
  },
};
</script>
